package org.android.brasil.projetos.spacehero.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.widget.TextView;

public abstract class BaseGameController {
	
	private float mGameTime;
	
	private float mCreateEnemyTime;
	
	private IGameEntity mShip;
	
	private List<IGameEntity> mShotList;
	
	private List<IGameEntity> mEnemyList;
	
	private GameEntityFactory mGameEntityFactory;
	
	protected void setShip(IGameEntity pGameEntity)
	{
		this.mShip = pGameEntity;
	}
	
	protected void addEnemy(IGameEntity pGameEntity){

		pGameEntity.setPosition(240.0f, 25.0f);
		this.mEnemyList.add(pGameEntity);
	}
	
	protected void removeEnemy(IGameEntity pGameEntity){

		this.mEnemyList.remove(pGameEntity);
	}
	
	protected void setGameEntityFactory(GameEntityFactory mGameEntityFactory)
	{
		this.mGameEntityFactory = mGameEntityFactory;
	}
	
	private void moveEnemy(final IGameEntity pEntity, final float pTime){
		
		pEntity.setPosition(pEntity.getX(), pEntity.getY() + 40 * pTime);
	}
	
	protected void createShip(){

		this.mShip.setRotation(180.0f);
		this.mShip.setPosition(240.0f, 774.0f);
	}
	
	protected void createShot(){}
	
	protected void createEnemy(){}
	
	protected void createNewShot(){}
	
	protected void createGameEntityFactory(){}
	
	private void updateGameTime(final float pTime){
		
		this.mGameTime += pTime;
		
		if(this.mGameTime >= this.mCreateEnemyTime)
		{
			this.createEnemy();
			this.mCreateEnemyTime = this.mGameTime + 10;
		}
	}
	
	private void updateShip(final float pTime){
		
	}
	
	private void updateShot(final float pTime){
		
	}
	
	private void updateEnemy(final float pTime){

		IGameEntity gameEntity;
		Iterator<IGameEntity> it;
		List<IGameEntity> enemyToDestroyList = new ArrayList<IGameEntity>();
		for (it = this.mEnemyList.iterator(); it.hasNext(); ){
			
			gameEntity = it.next();
			this.moveEnemy(gameEntity, pTime);
			
			if(gameEntity.getY() > 600){
				
				enemyToDestroyList.add(gameEntity);
			}
		}
		
		for (it = enemyToDestroyList.iterator(); it.hasNext(); ){
			
			this.removeEnemy(it.next());
		}
		
		enemyToDestroyList.clear();
	}
	
	private void processInput(){
		
	}
	
	private void checkShotCollision(){
		
	}
	
	public BaseGameController(){
		
	}
	
	public void initialize(){
		
		this.createGameEntityFactory();
		
		this.createShip();

		this.mShotList = new ArrayList<IGameEntity>();
		this.mEnemyList = new ArrayList<IGameEntity>();		
	}
	
	public void startGame(){

		this.mGameTime = 0;
		this.mCreateEnemyTime = 10;
	}
	
	public void updateGame(final float pTime){
		
		this.updateGameTime(pTime);
		
		this.processInput();

		this.updateShip(pTime);
		this.updateShot(pTime);
		this.updateEnemy(pTime);
		
		this.checkShotCollision();
		
		this.createNewShot();
	}
	
	public void setShipPosition(float pX){
		
		this.mShip.setPosition(pX, 774.0f);
	}
}
